package hua.sndandrcv;

import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

/**
 * 
 * 将在应用程序队列上发送消息的发送器应用程序
 * 
 * @author HuangHua
 * 
 */
public class Typesetter {
	@SuppressWarnings( { "unchecked", "deprecation" })
	public static void main(String args[]) {
		try {
			String hostName = "192.168.17.183";
			String channel = "SYSTEM.ADMIN.SVRCONN";
			String qManager = "QM_CRM_TEST";
			String qName = "QU_FOSS_ORDER_001_IN";
			int port = 1414;
			String userID = "mqm";
			String password = "mqm";

			/* 设置MQEnvironment 属性以便客户机连接 */
			MQEnvironment.hostname = hostName;
			MQEnvironment.channel = channel;
			// MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE MQ);
			MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_CLIENT);
			MQEnvironment.port = port;
			MQEnvironment.userID = userID;
			MQEnvironment.password = password;
			/* 连接到队列管理器 */
			MQQueueManager qMgr = new MQQueueManager(qManager);

			/* 设置打开选项以便打开用于输出的队列，如果队列管理器正在停止，我们也已设置了选项去应对不成功情况。 */
			int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

			/* 打开队列 */
			MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null,
					null);

			/* 设置放置消息选项我们将使用默认设置 */
			MQPutMessageOptions pmo = new MQPutMessageOptions();

			/* 创建消息，MQMessage 类包含实际消息数据的数据缓冲区，和描述消息的所有MQMD 参数 */

			/* 创建消息缓冲区 */
			MQMessage outMsg = new MQMessage();

			/* 设置MQMD 格式字段 */
			outMsg.format = MQC.MQFMT_STRING;

			/* 准备用户数据消息 */
			String msgString = "Test Message from PtpSender program, test by HuangHua ... ";
			outMsg.writeString(msgString);

			for (int i = 0; i < 500; i++) {
				/* 在队列上放置消息 */
				queue.put(outMsg, pmo);
			}
			/* 提交事务处理 */
			qMgr.commit();

			System.out.println(" The message has been Successfully put！\n");

			/* 关闭队列和队列管理器对象 */
			queue.close();
			qMgr.disconnect();
		} catch (MQException ex) {
			System.out.println("An MQ Error Occurred: Completion Code is :\t"
					+ ex.completionCode + "\n\n The Reason Code is :\t"
					+ ex.reasonCode);
			ex.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
